package org.xxpay.pay.channel.bee138;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Component;
import org.xxpay.common.http.HttpRequestTemplate;
import org.xxpay.common.util.Util;
import org.xxpay.common.util.sign.SignUtils;
import org.xxpay.core.entity.PayOrder;
import org.xxpay.core.entity.TransOrder;
import org.xxpay.pay.channel.BaseTrans;

import java.util.Map;

/**
 * @Package org.xxpay.pay.channel.bee138
 * @Class: Bee138TransNotifyService.java
 * @Description:
 * @Author leo
 * @Date 2019/5/26 11:18
 * @Version
 **/
@Component
public class Bee138TransService extends BaseTrans {

    private final static String logPrefix = "【优贝代付】";

    @Override
    public String getChannelName() {
        return Bee138Config.CHANNEL_NAME;
    }

    @Override
    public JSONObject trans(TransOrder transOrder) {
        _log.info("{}订单信息：{}", logPrefix, JSON.toJSONString(transOrder));
        Bee138Config bee138Config = new Bee138Config(getTransParam(transOrder));

        Map<String, Object> requestParameters = getPublicParameters(bee138Config, transOrder);
        requestParameters = getTransParameters(bee138Config, transOrder, requestParameters);
        _log.info("{}下单请求参数：{}", logPrefix, JSONObject.toJSONString(requestParameters));
        HttpRequestTemplate requestTemplate = new HttpRequestTemplate(null);
        JSONObject origin = requestTemplate.postForObject(Bee138Config.TRANSFER_URL, requestParameters, JSONObject.class);
        _log.info("{}下单响应参数：{}", logPrefix, origin.toJSONString());


        return null;
    }

    @Override
    public JSONObject balance(String payParam) {
        return null;
    }

    public String generateSign(Bee138Config bee138Config, Map<String, Object> parameters) {
        String signTxt = SignUtils.parameterText(parameters, SignUtils.SEPARATOR_AND);
        String sign = SignUtils.RSA_S.createSign(signTxt, bee138Config.getPrivateKey(), SignUtils.CHARSET_UTF8);

        _log.info("{}请求参数签名排序：{}，签名值：{}", logPrefix, signTxt, sign);

        return sign;
    }

    private Map<String, Object> getPublicParameters(Bee138Config bee138Config, TransOrder transOrder) {
        Map<String, Object> parameters = Maps.newHashMap();
        parameters.put("merchant_no", bee138Config.getMchId());
        // 接口版本
        parameters.put("interface_version", Bee138Config.DEFAULT_VERSION);
        // 签名方式
        //1.取值为：RSA或RSA-S，RSA使用pfx证书文件进行数据加密，RSA-S使用字符串密钥进行数据加密，商户需要从中选择一个值
        //2.该字段不参与签名
        parameters.put("sign_type", Bee138Config.DEFAULT_SIGN_TYPE);
        return parameters;
    }

    private Map<String, Object> getTransParameters(Bee138Config bee138Config, TransOrder transOrder, Map<String, Object> parameters) {
        // 商家网站生成的转账编号，由字母、数字组成
        parameters.put("mer_transfer_no", transOrder.getTransOrderId());
        // 请求码（转账：DMTI）
        parameters.put("tran_code", "DMTI");
        // 银行代码
        parameters.put("recv_bank_code", transOrder.getBankCode());
        // 收款人的银行账号
        parameters.put("recv_accno", transOrder.getAccountNo());
        // 收款人的姓名
        parameters.put("recv_name", transOrder.getAccountName());
        // 省份代码(见章节3.5)
        parameters.put("recv_province", transOrder.getProvince());
        // 城市代码(见章节3.5)
        parameters.put("recv_city", transOrder.getCity());
        // 转账金额（如100.23）
        parameters.put("tran_amount", Util.conversionYuanAmount(transOrder.getAmount()).toString());
        // 扣除手续费的方式：0：从转账金额中扣除  1：从账户余额中扣除
        parameters.put("tran_fee_type", bee138Config.getTradeRule());
        // 转账的方式（0：普通  1：加急）
        parameters.put("tran_type", bee138Config.getTransType());
        //
        parameters.put("sign_info", generateSign(bee138Config, parameters));

        return parameters;
    }

}
